<template>
  <div class="app-container">
    <el-form :model="goodsQueryParams" ref="queryFormRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户名称" prop="shopId">
        <el-select style="width: 100px" v-model="goodsQueryParams.shopId" placeholder="请选择商户名称" clearable>
          <el-option v-for="shop in shopList" :key="shop.shopId" :label="shop.shopName" :value="shop.shopId" />
        </el-select>
      </el-form-item>
      <el-form-item label="分类" prop="categoryId">
        <el-select style="width: 100px" v-model="goodsQueryParams.categoryId" placeholder="请选择分类名称" clearable>
          <el-option v-for="category in categoryList" :key="category.categoryId" :label="category.categoryName" :value="category.categoryId" />
        </el-select>
      </el-form-item>
      <el-form-item label="商品名" prop="goodsName">
        <el-input v-model="goodsQueryParams.goodsName" placeholder="请输入商品名" clearable />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['merchant:goods:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['merchant:goods:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['merchant:goods:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['merchant:goods:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="goodsId" />
      <el-table-column label="商品主图" align="center" prop="coverImage" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.coverImage" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="商户" align="center" prop="shopName" />
      <el-table-column label="分类" align="center" prop="categoryName" />
      <el-table-column label="商品名" align="center" prop="goodsName" />
      <el-table-column label="商品介绍" align="center" prop="goodsDesc" />
      <el-table-column label="商品售价" align="center" prop="price" />
      <el-table-column label="原价" align="center" prop="originalPrice" />
      <el-table-column label="库存" align="center" prop="stock" />
      <el-table-column label="销量" align="center" prop="salesCount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['merchant:goods:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['merchant:goods:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination :total="total" v-model:page="goodsQueryParams.pageNum" v-model:limit="goodsQueryParams.pageSize" @pagination="getList" />

    <edit-dialog ref="editDialogRef" v-model:show="dialog.show" :title="dialog.title" @success="getList"></edit-dialog>
  </div>
</template>

<script setup name="Goods" lang="ts">
// 类型声明
import type { Shop } from '@/types/shop';
import type { Category } from '@/types/category';
import type { Goods } from '@/types/goods';
interface QueryType {
  pageNum: number;
  pageSize: number;
}
interface GoodsQueryType extends QueryType {
  pageNum: number;
  pageSize: number;
  shopId: number;
  categoryId: number;
  goodsName: string;
}

import { listGoods, getGoods, delGoods, addGoods, updateGoods } from '@/api/merchant/goods.ts';
import { listCategory } from '@/api/merchant/category';
import { listShop } from '@/api/merchant/shop';
import editDialog from './editDialog.vue';

const { proxy } = getCurrentInstance();

const shopList = ref<Shop[]>([]);
const categoryList = ref<Category[]>([]);
const goodsList = ref<Goods[]>([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref('');
const queryFormRef = ref('');
const editDialogRef = ref('editDialog');

const goodsQueryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  shopId: null,
  categoryId: null,
  goodsName: '',
  goodsDesc: null,
  price: null,
  originalPrice: null,
  stock: null,
  coverImage: null,
  salesCount: null
});
// 新建、编辑弹窗
const dialog = reactive({
  show: false,
  title: '新增商品'
});
// const { queryParams, form, rules } = toRefs(data);

/** 查询店铺列表 */
function getShopList() {
  loading.value = true;
  listShop().then((response) => {
    shopList.value = response.rows;
    loading.value = false;
  });
}

/** 查询分类列表 */
function getCategoryList() {
  loading.value = true;
  listCategory().then((response) => {
    categoryList.value = response.rows;
    loading.value = false;
  });
}

/** 查询商品列表 */
function getList() {
  loading.value = true;
  listGoods(goodsQueryParams).then((response) => {
    goodsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  goodsQueryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryFormRef.value.resetFields();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  // console.log(dialog.show);
  dialog.show = true;
  dialog.title = '添加商品';
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _id = row.id || ids.value;
  getGoods(_id).then((res) => {
    if (res && res.data) {
      dialog.show = true;
      title.value = '修改商品';
      editDialogRef.value.setFormData(res.data);
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除商品编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delGoods(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess('删除成功');
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'merchant/goods/export',
    {
      ...queryParams.value
    },
    `goods_${new Date().getTime()}.xlsx`
  );
}

provide('shopList', shopList);
provide('categoryList', categoryList);
provide('resetQuery', resetQuery);

getList();
getShopList();
getCategoryList();
</script>
